package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.CaseService;
import com.tgu.home_action.service.FaceService;
import com.tgu.home_action.service.ThirdPartyService;
import com.tgu.home_action.service.admin.AdminCaseService;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "第三方接口")
@RestController
@RequestMapping("/thirdParty")
public class ThirdPartyController {
    @Autowired
    private ThirdPartyService thirdPartyService;
    @Autowired
    private Logger logger;
    @Autowired
    private AdminCaseService adminCaseService;
    @Autowired
    private FaceService faceService;
    @Autowired
    private CaseService caseService;

    @ApiOperation("404页面")
    @GetMapping("/notFound")
    public ResponseData<List<Case>> notFound(@RequestParam Integer count) {
        return thirdPartyService.notFound(count);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseData<Client> login(@Validated(Client.login.class) @RequestBody Client client) {
        return thirdPartyService.login(client);
    }

    @ApiOperation("更新Token")
    @PostMapping("/refresh")
    public ResponseData<Object> refreshToken(@ApiJsonObject(name = "ClientRefreshToken",
            value = @ApiJsonProperty(key = "refreshToken", description = "refreshToken")) @RequestBody Map<String, Object> param) {
        try {
            return thirdPartyService.refreshToken((String) param.get("refreshToken"));
        } catch (ExpiredJwtException e) {
            logger.warn("token已过期");
            logger.error(e.getMessage());
            return new ResponseData<>(ResponseState.REFRESH_TOKEN_IS_EXPIRED.getMessage(), ResponseState.REFRESH_TOKEN_IS_EXPIRED.getValue());
        } catch (Exception e) {
            System.out.println(e);
            logger.error("发生错误");
            logger.error(e.getMessage());
            return new ResponseData<>(ResponseState.REFRESH_TOKEN_IS_ERROR.getMessage(), ResponseState.REFRESH_TOKEN_IS_ERROR.getValue());
        }
    }

    @ApiOperation("查询个人信息")
    @GetMapping
    public ResponseData<Client> getClient(@ApiIgnore @ModelAttribute("clientUser") Client client) {
        return thirdPartyService.getClient(client);
    }

    @ApiOperation("查询所有案件信息（可以根据id,familyId,oldManId,state,province,city,district筛选）")
    @GetMapping("/case")
    public ResponseData<Page<Case>> getCases(Case caseInformation) {
        return thirdPartyService.getCases(caseInformation);
    }


    @ApiOperation("人脸识别")
    @PostMapping("/face")
    public ResponseData<Face> faceIdentification(@ApiJsonObject(name = "ClientFaceIdentification", value = {
            @ApiJsonProperty(key = "imgUrl", description = "照片地址"),
            @ApiJsonProperty(key = "appId", description = "appId"),
            @ApiJsonProperty(key = "accessKey", description = "accessKey"),
    }) @RequestBody Map<String, Object> param) {
        String imgUrl = (String) param.get("imgUrl");
        String appId= (String) param.get("appId");
        String accessKey= (String) param.get("accessKey");
        return faceService.clientFaceIdentification(imgUrl, appId,accessKey);
    }

    @ApiOperation("通过ID查询案件")
    @GetMapping("/case/id")
    public ResponseData<Case> getCaseById(@RequestParam Integer caseId) {
        return thirdPartyService.getCaseById(caseId);
    }

}
